package com.polaris.live.popup

import android.content.Context
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.SpanUtils
import com.blankj.utilcode.util.StringUtils
import com.bumptech.glide.Glide
import com.lxj.xpopup.core.CenterPopupView
import com.polaris.live.R
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.databinding.PopupSendGiftBinding
import com.polaris.live.resp.bean.GiftSendBean
import com.polaris.live.translation.R.string
import com.polaris.live.utils.XPopupUtils
import com.polaris.live.utils.image.ImageUtils

/**
 * 送礼确认弹窗
 *
 * @author Created by 天晴 on 2024/8/7 14:25
 * @since 1.0.0
 **/
class SendGiftSurePopup(
    context: Context,
    private val giftSendBean: GiftSendBean,
    private val onOk: (() -> Unit)? = null,
) : CenterPopupView(context) {

    private lateinit var mBinding: PopupSendGiftBinding
    override fun getImplLayoutId(): Int {
        return R.layout.popup_send_gift
    }

    override fun onCreate() {
        super.onCreate()
        mBinding = PopupSendGiftBinding.bind(popupImplView)

        Glide.with(context)
            .load(ImageUtils.imgPath(giftSendBean.gift.pic))
            .centerCrop()
            .into(mBinding.iconImg)

        val list = StringUtils.getString(string.what_currently_giving).split("#")
        val span = SpanUtils.with(mBinding.message)
            .append(list[0])
            .setForegroundColor(ColorUtils.getColor(R.color.f202028))
            .append("${giftSendBean.gift.giftName}*${giftSendBean.num}")
            .setForegroundColor(ColorUtils.getColor(R.color.color_ffa100))
            .append(list[1])
            .setForegroundColor(ColorUtils.getColor(R.color.f202028))
            .append("${giftSendBean.num * giftSendBean.gift.coin}")
            .setForegroundColor(ColorUtils.getColor(R.color.color_ffa100))
            .append(list[2])
            .setForegroundColor(ColorUtils.getColor(R.color.f202028))
            .create()

        mBinding.message.text = span


        mBinding.tvCancel.setOnSingleClickListener {
            dismiss()
        }
        mBinding.tvOk.setOnSingleClickListener {
            dismiss()
            onOk?.invoke()
        }
    }

    companion object {

        fun show(
            context: Context,
            giftSendBean: GiftSendBean,
            onOk: (() -> Unit)? = null,
        ) {
            XPopupUtils.showViewPopup(
                SendGiftSurePopup(context, giftSendBean, onOk),
                {
                    hasShadowBg(true)
                },
            )
        }
    }
}